import {
	getOpenId
} from '../../utils/getOpenid.js'
import {
	get,
	post
} from '../../utils/request.js'
const openid = ''
Page({
	data: {
		homePlaceholder: '请输入想搜索的商品名称', // map页的input提示问题
		isInputValue: false,
		openid: '',
		historyData: [],
		hotData: [],
		icon: 'icon-yanjing',
		eyesFlag: true,
		inputKeyword: '',
		tipsData: [],
		order: '',
		listData: [],
		topIconIsShow: false, // 控制滚动到top的icon的显示
		nowIndex: 0,
		arricon: 'icon-plus-shiftdown'
	},
	onLoad: function(options) {
		getOpenId()
		this.openid = wx.getStorageSync('OPENID') || ''
		// console.log(this.openid)
		this.getHotData()
	},
	onCancel() {
		wx.switchTab({
			url: '/pages/home/home'
		})
	},
	async getHotData() {
		const data = await get('/search/indexaction?openId=' + this.openid)
		// console.log(data)
		this.setData({
			historyData: data.historyData,
			hotData: data.hotKeywordList
		})
	},
	changeEyes() {
		let eyesFlag = this.data.eyesFlag
		if (eyesFlag) {
			this.setData({
				eyesFlag: false,
				icon: 'icon-biyanjing'
			})
		} else {
			this.setData({
				eyesFlag: true,
				icon: 'icon-yanjing'
			})
		}
	},
	inputFocus() {
		this.setData({
			listData: [],
			inputKeyword: ''
		})
	},
	onInput(e) {
		let inputKeyword = e.detail.value
		if (!inputKeyword) { // 没有内容就不显示删除icon
			this.setData({
				isInputValue: false
			})
		} else { // 如果输入了 有内容 就显示删除icon
			this.setData({
				isInputValue: true
			})
		}
		// console.log(this.data.inputKeyword)
		this.setData({
			inputKeyword: inputKeyword
		})
		this.getSearch()
	},
	async getSearch() {
		const data = await get('/search/helperAction', {
			keyword: this.data.inputKeyword
		})
		// console.log(data)
		this.setData({
			tipsData: data.keywords
		})
	},
	async searchWords(e) {
		let value = e.currentTarget.dataset.value
		// console.log(value)
		this.setData({
			inputKeyword: value || this.data.inputKeyword
		})
		const data = await post('/search/addhistoryaction', {
			openId: this.openid,
			keyword: value || this.data.inputKeyword
		})
		// console.log(data)
		this.getHotData()
		this.getListData()
	},
	async getListData() {
		const data = await get('/search/helperAction', {
			keyword: this.data.inputKeyword,
			order: this.data.order
		})
		// console.log(data)
		this.setData({
			listData: data.keywords,
			tipsData: []
		})
	},
	clearSearch() {
		// console.log(1)
		this.setData({
			inputKeyword: '',
			listData: []
		})
	},
	del() {
		let that = this
		wx.showModal({
			title: '操作提示',
			content: '确认删除全部历史记录?',
			success: function(res) {
				if (res.confirm) {
					that.delAction()
				}
			}
		})
	},
	async delAction() {
		const data = await post('/search/clearHistoryAction', {
			openId: this.openid // 清除对应openid的记录
		})
		// console.log(data)
		// 后端清除历史数据后 前端这样处理
		if (data) { // data 为true 是 返回清除成功的情况
			this.setData({
				historyData: []
			})
		}
	},
	changeNav (e) {
		let index = e.currentTarget.dataset.index
		this.setData({
			nowIndex: index
		})
		// console.log(index)
		if (index == 2) {
			if (this.data.order == 'asc') {
				this.setData({
					order: 'desc',
					arricon: 'icon-plus-shiftdown'
				})
			} else {
				this.setData({
					order: 'asc',
					arricon: 'icon-plus-shiftup'
				})
			}
		} else {
			this.setData({
				order: ''
			})
		}
		this.getListData()
	},
	goToDetail (e) {
		let id = e.currentTarget.dataset.id
		// console.log(id)
		wx.navigateTo({
			url: `/pages/goodsdetail/goodsdetail?id=${id}`
		})
	},
	onPageScroll(e) { // page页面的监听滚动的事件
		if (e.scrollTop > 100) { // 如果滚动大于100 就显示top组件
			this.setData({
				topIconIsShow: true
			});
		} else {
			this.setData({
				topIconIsShow: false
			});
		}
	},
})
